package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.BrandDao;
import com.atguigu.gulimail.product.dao.CategoryDao;
import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimail.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimail.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    //自动注入商品分类dao
    @Resource
    CategoryDao categoryDao;
    //自动注入品牌dao
    @Resource
    BrandDao brandDao;

    @Autowired
    BrandService brandService;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    //保存品牌和分类的详细信息
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //先从categoryBrandRelation实体中进行获取商品分类和品牌相关联实体的中 里面属性商品品牌id和商品分类id
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //根据id 使用各自的dao接口进行调用根据各自id获取品牌实体 商品分类的实体
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        BrandEntity brandEntity = brandDao.selectById(brandId);
        //从数据库获取的实体里面各自商品分类的名称和品牌名称设置到 商品分类和品牌相关联的实体
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        //该实现类接口调用save方法， categoryBrandRelation实体保存到pms_category_brand_relation标中
        this.save(categoryBrandRelation);


    }

    //修改关联表中的根据品牌id进行修改品牌名称
    @Override
    public void updateBrand(Long brandId, String name) {
        //创建品牌和实体相关联实体
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        //修改的品牌id和品牌的名称设置到实体属性中
        relationEntity.setBrandName(name);
        relationEntity.setBrandId(brandId);
        //设置修改条件 brand_id = brandId 进行修改关联表的记录
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }


    //修改商品分类和品牌关联的级联的商品分类名称和商品分类id
    @Override
    public void updateCascade(Long catId, String name) {

        this.baseMapper.updateCategory(catId,name);
    }

    //根据三级分类的id进行获取 分类关联下 品牌
    @Override
    public List<BrandEntity> getBrandCatId(Long catId) {
        //使用品牌分类关联的dao接口 按照  三级分类id 条件进行查询 获取到品牌分类实体的关联实体
        List<CategoryBrandRelationEntity> relationEntities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //获取关联实体list容器进行流式处理
        List<BrandEntity> brandList = relationEntities.stream().map(
                (item) -> {
                    //品牌的dao接口调用根据品牌id获取到品牌实体， 品牌id 从关联实体中获取
                    BrandEntity brandEntity = brandService.getById(item.getBrandId());
                    return brandEntity;
                }
        ).collect(Collectors.toList());
        //返回查询品牌实体list容器
        return brandList;
    }

}